<?php
/**
 * Clients functions and definitions
 *
 */

add_action( 'init', 'client_posttype_init' );
if ( !function_exists( 'client_posttype_init' ) ) :
function client_posttype_init() {

	$client_labels = array(
		'name' => _x('Clients', 'post type general name', 'cingle'),
		'singular_name' => _x('Client', 'post type singular name', 'cingle'),
		'add_new' => _x('Add New', 'client'),
		'add_new_item' => __('Add New Client'),
		'edit_item' => __('Edit Client'),
		'new_item' => __('New Client'),
		'all_items' => __('All Clients'),
		'view_item' => __('View Client'),
		'search_items' => __('Search Clients'),
		'not_found' =>  __('No clients found'),
		'not_found_in_trash' => __('No clients found in Trash'), 
		'parent_item_colon' => '',
		'menu_name' => __('Clients')
	);

	$client_args = array(
		'labels' => $client_labels,
		'public' => false,
		'publicly_queryable' => false,
		'show_ui' => true, 
		'show_in_menu' => true, 
		'query_var' => false,
		'rewrite' => false,
		'capability_type' => 'post',
		'has_archive' => false, 
		'hierarchical' => false,
		'menu_position' => '5.4',
		'menu_icon' => CINGLE_URI . 'img/posttype-client.png',
		'supports' => array( 'title', 'thumbnail' )
	); 
	register_post_type( 'client', $client_args );

}
endif;

// Add metabox
add_action('admin_init', 'client_metabox');
function client_metabox(){
	add_meta_box('client-metabox', 'Client Data', 'client_metabox_callback', 'client', 'normal');
}

// Metabox callback
function client_metabox_callback() { ?>
	<input type="hidden" name="dw_simplex_post_id" id="dw_simplex_post_id" value="<?php echo get_the_ID(); ?>" />
	<table width="100%">	
		<tr class="alternate">
			<th class="left"><?php _e('Client URL','dw-simplex');  ?></th>
			<td>
				<p><input type="text" name="client-url" id="client-url" class="regular-text" value="<?php echo get_post_meta(get_the_ID(), '_client_url', true); ?>" ></p>
			</td>
		</tr>
	</table>
<?php }

// Action when save post
add_action('save_post', 'admin_save_client');
function admin_save_client($post_id){
	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
		return;
	if( isset($_POST['post_type']) && 'client' == $_POST['post_type'] ){
		if( !empty($_POST['client-url']) ){
			update_post_meta($post_id, '_client_url', $_POST['client-url']);
		}
	}
}